package com.example.gtsp.hwdemo.controller;

import com.example.gtsp.hwdemo.service.RabbitMQService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/mall/rabbitmq")
public class RabbitMQController {
    @Resource
    private RabbitMQService rabbitMQService;
    Logger LOG = LoggerFactory.getLogger(RabbitMQController.class);
    /**
     * 发送消息
     */
    @PostMapping("/sendMsg")
    public String sendMsg(@RequestParam(name = "msg") String msg) throws Exception {
        LOG.info("sendMsg,{}",msg);
        return rabbitMQService.sendMsg(msg);
    }

    @GetMapping("/testMsg")
    public String testMsg(@RequestParam(name = "msg") String msg) throws Exception {
        LOG.info("testMsg,{}",msg);
        return "测试成功，发送的消息是："+msg;
    }

    @PostMapping("/publish")
    public String publish(@RequestParam(name = "msg") String msg) throws Exception {
        return rabbitMQService.sendMsgByFanoutExchange(msg);
    }
    /**
     * 通配符交换机发送消息
     *
     * @author java技术爱好者
     */
    @PostMapping("/topicSend")
    public String topicSend(@RequestParam(name = "msg") String msg, @RequestParam(name = "routingKey") String routingKey) throws Exception {
        return rabbitMQService.sendMsgByTopicExchange(msg, routingKey);
    }
    @PostMapping("/headersSend")
    @SuppressWarnings("unchecked")
    public String headersSend(@RequestParam(name = "msg") String msg,
                              @RequestParam(name = "json") String json) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> map = mapper.readValue(json, Map.class);
        LOG.info("headersSend接收的json：{}"+json);
        return rabbitMQService.sendMsgByHeadersExchange(msg, map);
    }


}
